package src.main.java.cn.edu.sdjzu.xg.eagle.service.impl;

import cn.edu.sdjzu.xg.eagle.entity.ActorType;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import src.main.java.cn.edu.sdjzu.xg.eagle.dao.StudentDao;
import src.main.java.cn.edu.sdjzu.xg.eagle.entity.Student;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;

public class StudentServiceImpl implements StudentService {
    static StudentDao studentDao = null;
    static UserService userService = null;

    static {
        try{
        studentDao = DaoFactory.getDao(StudentDao.class, "StudentDaoImpl");
        actorServide = ServiceFactory.getService(ActorService.class, "ActorServiceImpl");
        userService = ServiceFactory.getService(UserService.class, "UserServiceImpl");
         }catch(Exception e){
            e.printStackTrace();
         }
    }
    @Override
    public Student find(Long id)throws SQLException, BaseException,InvocationTargetException{
        Connection connection = null;
        try{
            connection = JdbcHelper.getConn();
            Student student = studentDao.find(connecton,id);
            return student;
        }finally{
          JdbcHelper.close(connection);
        }
    }
}
//20230214030-王桉锦
    public boolean addStudent(Student student)throws SQLException, BaseException, InvocationTargetException{
    Connection connection = null;
    try{
        connection= JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Long studentId = student.getLongId();
        student.setId(studentId);
        User user =new User();
        user.setUsername(student.getNo());
        user.setPassword(student.getNo());
        user.setActorType(ActorType.STUDENT);
        user.setActor(student);
        Integer UserId = userService.add(connection,user);
        if(UserId!=null){
            throw new BaseException("添加用户失败");

        }
        user.setId(userId);
        student.setUser(user);
        boolean isAdded=studentDao.addStudent(connection,student);
        if(!isAdded){
            throw new BaseException("添加学生失败");

        }
        connection.commit();
        return true;


    }catch (SQLException|BaseException|InvocationTargetException e){
        if（connection!=null){
              connection.rollback();
        }
        e.printStackTrace();
        throw new BaseException("数据库操作异常");
        }finally {
        JdbcHelper.close(connection);

        }
    }
    //202302104030-王桉锦
    @Override
    public boolean delete(Long id)throws SQLException,BaseException,InvocationTargetException{
    Connection connection = null;
    try{
        if (id==null||id==0){
            throw new BaseException("无效学生ID："+id);

        }
        connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean isUserDeleted = userService.delete(connection,id);
        if(!isUserDeleted){
            connection.rollback();
            throw new BaseException("删除关联用户失败");
        }
        connection.commit();
        return true;
    }catch (SQLException e) {
        if (connection != null && !connection.isClosed()) {
            connection.rollback();
        }
        throw new SQLException("数据库操作异常：" + e.getMessage(), e);
        }catch(BaseException e) {
        throw e;
        }catch (Exception e){
            throw new InvocationTargetException(e,"删除操作失败");
    }finally {
        JdbcHelper.close(connection);
    }
    }
